<?php
if(isset($_POST['action']))
{
    $db = new Database();
    $page = $_POST['page'];
    $cur_page = $page;
    $page -= 1;
    $per_page = 10; // Per page
    $previous_btn = true;
    $next_btn = true;
    $first_btn = true;
    $last_btn = true;
    $start = $page * $per_page;
    $action = $_POST['action'];
    //sort
    $markSortUser = @$_POST['markBalAudit'];//for toggle of desc and asc 
    $fieldToSort = @$_POST['sortBalRel']; //field to sort
    $sortfield = " ORDER BY id "; // default field to sort
    if(!empty($fieldToSort)){
	$sortfield = " ORDER BY $fieldToSort ";
    }
    $sortType = " ASC ";
    //condition for sorting if desc or asc
    if($markSortUser%2==0){
	$sortType = " ASC ";
    }else{
	$sortType = " DESC ";
    }
    $sort =  $sortfield.$sortType; // concatinate sort
    //echo $sort;
    
    
    $finaldata = "<table border='0' id='table_accomodation' class='table_accomodation'>
			    <thead>
				<tr>
                                    <th class='sortdealer' rel='dealerCode'>Dealer Code</th>
				    <th class='sortdealer' rel='firstname'>Dealer's Name</th>
				    <th class='sortdealer' rel='id' colspan='2'>Actions</th>
				</tr>
			    </thead>
			    <tbody id='searchDealerName'>";
    if($_POST['action']=="searchDealerName")
    {
        $value = $_POST['value'];
	$queryString = "SELECT DISTINCT(dealerCode), branchCode,lastName,firstName,middleName,
			     pointsEarned,CONCAT_WS(' ', firstName, lastName) AS name
			     FROM tbdealer where firstName like '%$value%' OR lastName like '%$value%'
			     OR dealerCode like '%$value%' OR CONCAT_WS(' ', firstName, lastName)
			     LIKE '%$value%' group by dealerCode";
        $dealer = $db->query($queryString.$sort." LIMIT $start, $per_page","array");
	$count = $db->query($queryString, "numrows");
	$no_of_paginations = ceil($count / $per_page);
    }elseif($_POST['action']=="default"){
        //for default dealers name
        $queryString = "SELECT DISTINCT(dealerCode),branchCode,lastName,firstName,middleName,pointsEarned
			    FROM tbdealer group by dealerCode";
        $dealer = $db->query($queryString.$sort." LIMIT $start, $per_page","array");
	$count = $db->query($queryString, "numrows");
	$no_of_paginations = ceil($count / $per_page);
        
    }
    if($count>0){
      foreach($dealer as $data)
        {
        $finaldata .= '<tr>
            
            <td>'.$data['dealerCode'].'</td>
            <td>
                '.$data["firstName"]. " " .$data["lastName"].'
            </td>
            <td class="actions">
                <img src="/images/icon/view_points.png" title="View your Points by Category" class="cursor" id="viewDealerPoints" rel="'.$data["dealerCode"].'"/>
            </td>
            <td class="actions">
                <img src="/images/icon/redeem_prodList.png" title="View List of Points" class="viewAllProductRedeemed cursor" rel="'.$data["dealerCode"].'"/>
            </td>
        </tr>';
        }
        //pagination
        pagination($cur_page,$page,$per_page,$start,$previous_btn,$next_btn,$first_btn,$last_btn,$no_of_paginations,$finaldata,$count,$action,$value);
    }else{
        $finaldata.="<tr><td colspan='4'><center>No Record Found</center></td></tr>";
        echo $finaldata;
    }
}